<?php
/**
 * 具体的商品所属服务信息 model 类
 *
 * @author <judasnow@gmail.com>
 */
require_once( APPPATH . 'models/base_model.php' );

class Product_at_spec_class_model extends Base_model{

	private $_CI;
	private $_table_name;

	public function __construct(){
		$this->_CI =& get_instance(); 
		$this->_table_name = 'product_at_spec_class';
		parent::__construct( $this->_table_name );
	}

	//根据指定的 product_id(源) 在 spec 表中获取
	//被覆盖之后的列表
	public function find_all_with_spec_class_info( $cond = array() , $order_by = '' , $per_pager = '' , $offset = '' ){
		$raw = $this->find_all( $cond , $order_by , $per_pager , $offset );
		if( empty( $raw ) ){
			 return '';
		}
		$this->_CI->load->model( 'product_class_model' , '' , TRUE );
		//获取所有的服务信息
		$all_product_class = $this->product_class_model->find_all();
		foreach( $raw as $index=>$product_at_spec_class ){
			 foreach( $all_product_class as $no=>$product_class ){
				 if( $product_at_spec_class['product_class_id'] == $product_class['class_id'] ){
				 	 $raw[$index]['product_class_name_cn'] = $product_class['product_class_name_cn'];
					 break;
				 }
			 }
			 if( empty( $raw[$index]['product_class_name_cn'] ) ){
				 //不应该为一个异常
				 //throw new Exception( '尝试查找了class_id为[ ' . $product_class['class_id'] . ' ]的服务,但是在product_class表中,该记录并不存在' );
				 $raw[$index]['product_class_name_cn'] = "[已删除的服务]";
			 }
		}
		return $raw;
	}
	public function find_all( $cond = array() , $order_by = '' , $per_pager = '' , $offset = '' , $search_sql = '' ){
		 if( !empty( $search_sql ) ){
			 $sql_limit = !empty( $per_pager ) ? "LIMIT $per_pager" : '';
			 $sql_offset = !empty( $offset ) ? " OFFSET $offset" : ''; 
			 $sql = $search_sql . " $sql_limit $sql_offset";
			 $q = $this->db->query( $sql );
			 $ori_product_infos = $q->result_array();	 
		 }else{
			 if( !empty( $order_by['title'] ) && !empty( $order_by['order'] ) ){
				 $this->db->order_by( $order_by['title'] , $order_by['order'] );
			 }
			 if( !empty( $cond ) ){
				 $this->db->where( $cond );		 
			 }
			 $res = $this->_CI->db->get( $this->_table_name , $per_pager , $offset );
			 $ori_product_infos = $res->result_array();
		 }
		 return $ori_product_infos;
        }

        public function del_all_by_product_id( $product_id ){
                parent::del( array( 'product_id'=>$product_id ) );
        }
}
